package action;


import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.hibernate.Session;
import org.hibernate.Transaction;

import classes.Employe;


import cal.Entry;
import cal.TableBean;

import sessionfactory.HibernateSessionFactory;

import java.text.SimpleDateFormat;


public class AddEventAction extends Action{
	
	public static Date stringToDate(String sDate, String sFormat) 
	throws Exception 
	{
        SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
        return sdf.parse(sDate);
	} 
	
	public static String stringToDateSQL(String sDate) 
	throws Exception 
	{
		SimpleDateFormat toret = new SimpleDateFormat("yyyy-MM-dd");
		
		Date res = stringToDate(sDate, "MM/dd/yyyy");
		
        return toret.format(res);
	} 
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
			{
				Session session = HibernateSessionFactory.getSessionFactory().openSession();
				Transaction tx = session.beginTransaction();
				Entry toadd = new Entry ();
				
				
				request.setAttribute("idOfEmploye", request.getSession().getAttribute("idOfEmploye"));
				TableBean t = (TableBean)request.getSession().getAttribute("table");
				Date d = stringToDate(t.getDate(), "MM/dd/yyyy");
				Date tt = new Date ();
				//toadd.setIdEmploye(Integer.valueOf(request.getParameter("idOfEmploye")));
				
				Object i = request.getSession ().getAttribute("idOfEmploye");
				toadd.setIdEmploye(Integer.valueOf(i.toString()));
				toadd.setDescription(request.getParameter("description"));
				toadd.setHour(request.getParameter("time"));
				toadd.setDate(d);
				
				session.save(toadd);
				tx.commit();
				return mapping.findForward("Success");
			}
	
}
